package com.tea.room.service.wechat.impl;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tea.room.dao.WeChatTokenDao;
import com.tea.room.model.Return;
import com.tea.room.model.WeChatToken;
import com.tea.room.service.wechat.MenuService;
import com.tea.room.util.ConfigUtil;
import com.tea.room.util.HttpClient;

@Service
public class MenuServiceImpl implements MenuService {
    
    private static Logger logger = LoggerFactory.getLogger(PayServiceImpl.class);

    private static Properties wechat = ConfigUtil.getProperties("wechat.properties");
    
    @Autowired
    private WeChatTokenDao weChatTokenDao;
    
    @Override
    public Return addMenu(HttpServletRequest request) {
        Return rn = new Return();
        try {
            WeChatToken token = weChatTokenDao.queryWCToken();
            String url = wechat.getProperty("MENU_ADD") + token.getAccess_token();
            HttpClient client = HttpClient.getInstance();
            String menu = request.getParameter("menu");
            String post = client.sendHttpPost(url, menu);
            rn.setIsSuccessflag(true);
            rn.setMessage(post);
        } catch (Exception e) {
            rn.setIsSuccessflag(false);
            rn.setMessage(e.getMessage());
            e.printStackTrace();
        }
        return rn ;
    }

    @Override
    public Return queryMenu(HttpServletRequest request) {
        Return rn = new Return();
        try {
            WeChatToken token = weChatTokenDao.queryWCToken();
            String url = wechat.getProperty("MENU_QUERY") + token.getAccess_token();
            HttpClient client = HttpClient.getInstance();
            String post = client.sendHttpGet(url);
            rn.setIsSuccessflag(true);
            rn.setMessage(post);
        } catch (Exception e) {
            rn.setIsSuccessflag(false);
            rn.setMessage(e.getMessage());
            e.printStackTrace();
        }
        return rn ;
    }

    @Override
    public Return removeMenu(HttpServletRequest request) {
        Return rn = new Return();
        try {
            WeChatToken token = weChatTokenDao.queryWCToken();
            String url = wechat.getProperty("MENU_DELETE") + token.getAccess_token();
            HttpClient client = HttpClient.getInstance();
            String post = client.sendHttpGet(url);
            rn.setIsSuccessflag(true);
            rn.setMessage(post);
        } catch (Exception e) {
            rn.setIsSuccessflag(false);
            rn.setMessage(e.getMessage());
            e.printStackTrace();
        }
        return rn ;
    }

}
